﻿using System.Threading;
using Game;
using GameFramework.common;
using GameFramework.net;
using GameFramework.net.msgs;
using GameFramework.net.nettest;
using GameFramework.UI;
using UnityEngine.UIElements;

namespace Resources.UI.NetTest
{
    public class NetTestView:BaseView
    {
        [UIBind(ClassName = "select")]
        public VisualElement select;

        [UIBind(Name = "selectServerBtn")]
        public Button selectServerBtn;
        
        [UIBind(Name = "selectClientBtn")]
        public Button selectClientBtn;
        
        [UIBind(Name = "server")]
        public VisualElement server;
        
        [UIBind(ClassName = "startSertBtn")]
        public Button startSertBtn;
        [UIBind(ClassName = "serverStatus")]
        public Label serverStatus;
        
        [UIBind(ClassName = "collectNum")]
        public Label collectNum;
        
        [UIBind(Name = "client")]
        public VisualElement client;
        [UIBind(ClassName = "clientConnectStatus")]
        public Label clientConnectStatus;
        [UIBind(ClassName = "clientName")]
        public TextField clientName;
        
        [UIBind(ClassName = "connectBtn")]
        public Button connectBtn;

        public override void init()
        {
            base.init();
            selectServerBtn.RegisterCallback<ClickEvent>(evt =>
            {
                select.style.display = DisplayStyle.None;
                server.style.display = DisplayStyle.Flex;
            });
            selectClientBtn.RegisterCallback<ClickEvent>(evt =>
            {
                select.style.display = DisplayStyle.None;
                client.style.display = DisplayStyle.Flex;
            });
            startSertBtn.RegisterCallback<ClickEvent>(evt =>
            {
                GameMain.Instance.startTcpServer();
            });
            
            connectBtn.RegisterCallback<ClickEvent>(evt =>
            {
                string text = clientName.text;
                if (text.Equals(""))
                {
                    connectBtn.text = "客户端名未设置，无法连接！";
                    return;
                }
                //连接客户端
                TcpClient.Instance.Connect(() =>
                {
                    clientConnectStatus.text = "连接成功";
                    NetManager.Instance.isClient = true;
                    //TODO 发送登录消息
                    C2S_Login loginMsg = new C2S_Login();
                    loginMsg.name = text;
                    TcpClient.Instance.sendMsg(loginMsg);
                }, (e) =>
                {
                    clientConnectStatus.text = e.Message;
                });
            });
            addEventListener();
        }

        private void addEventListener()
        {
            EventManager.Instance.rigisterListener(typeof(ClientChangeEvent), e =>
            {
                if (NetManager.Instance.isClient)
                {
                    return;
                }

                int clientCount = (e as ClientChangeEvent).nowClientNum;
                collectNum.text = "连接人数：" + clientCount;
            });
            EventManager.Instance.rigisterListener(typeof(TcpServeStatusChange), e =>
            {
                TcpServeStatusChange statusChange = e as TcpServeStatusChange;
                int status = statusChange.status;
                if (status == 1)//启动成功
                {
                    startSertBtn.style.visibility = Visibility.Hidden;
                }
                else
                {
                    startSertBtn.style.visibility = Visibility.Visible;
                }

                string msg = statusChange.msg;
                serverStatus.text = msg;
            });
        }
    }
}